package com.example.drools.spring.yc.service.impl;

import com.example.drools.spring.entity.CheckResult;
import com.example.drools.spring.yc.constants.StatusTag;
import com.example.drools.spring.yc.core.YcRuleEngine;
import com.example.drools.spring.yc.service.YcCheckNullService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author ddsyw
 * @version 1.0
 * @date 2021/12/31 11:04 上午
 */
@Service
@Slf4j
public class YcCheckNullServiceImpl implements YcCheckNullService {

    @Resource
    private YcRuleEngine ycRuleEngine;

    @Override
    public Map<String, List<Map<String, String>>> checkNulls(List<Map<String, String>> maps) {
        Map<String, List<Map<String, String>>> listMap = new HashMap<>(2);
        List<Map<String, String>> success = new ArrayList<>();
        List<Map<String, String>> fail = new ArrayList<>();
        maps.forEach(map -> {
            CheckResult checkResult = new CheckResult();
            ycRuleEngine.executeRuleEngine(checkResult, map);
            if (checkResult.isOk()) {
                success.add(map);
            } else {
                fail.add(map);
            }
        });
        listMap.put(StatusTag.SUCCESS.getStatus(), success);
        listMap.put(StatusTag.FAIL.getStatus(), fail);
        return listMap;
    }

    @Override
    public List<Map<String, String>> checkNullReturnSuccess(List<Map<String, String>> maps) {
        Map<String, List<Map<String, String>>> listMap = checkNulls(maps);
        return listMap.get(StatusTag.SUCCESS.getStatus());
    }
}
